package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.qo.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceimpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void save(Customer customer) {
        //获得当前登录用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //设置录入人
        customer.setInputuser(employee);
        //设置销售员
        customer.setSeller(employee);
        //设置录入时间
        customer.setInputTime(new Date());
        customerMapper.insert(customer);

    }

    @Override
    public void deleteByPrimaryKey(Long id) {

        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByPrimaryKey(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

    @Override
    public Customer get(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> selectAll() {
        return customerMapper.selectAll();
    }

    @Override
    public PageInfo query(CustomerQuery qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"input_time desc");
        List<Customer> list = customerMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public String checkTel(String tel) {
        String checkTel = customerMapper.checkTel(tel);
        return checkTel;
    }

    @Override
    public Customer checkTelById(String tel, Long id) {
        Customer customer =customerMapper.checkTelById(tel,id);
        return customer;
    }

    @Override
    public List<Customer> selectByStatusId(int i,int k) {
        List<Customer> list = customerMapper.selectByStatusId(i,k);
        return list;
    }

    @Override
    public PageInfo queryPool(CustomerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Customer> list= customerMapper.selectPool(qo);
        return new PageInfo(list);
    }
}
